package com.wq.biz;

import com.wq.ui.coms.BasicAnimPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * 骰子控制类
 *
 * @author wq
 * 2020/12/19 - 17:43
 */
public class Dice extends BasicAnimPanel {
    private boolean canRoll = false;
    public static int nextRoll = -1;

    private int index = 0;

    public Dice(Icon... icons) {
        super(icons);
        super.init(3, 3);
        this.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                if (canRoll) {
                    setCanRoll(false);
                    roll();
                }
            }
        });
    }


    //掷骰子

    public void roll() {
        super.start();
        int rollNum = (int) (Math.random() * 6) + 1;
        if (!(nextRoll == -1)) {
            rollNum = nextRoll;
            nextRoll = -1;
        }
        if (index == 0) {
            rollNum = 6;
            index++;
        }
        super.setLastShowIcon(rollNum - 1);

        final int rollN = rollNum;
        new Thread() {
            public void run() {
                while (true) {
                    if (isOver()) {
                        Manager.getManager().hasThrowDice(rollN);

                        System.err.println(rollN);
                        return;
                    } else {
                        System.out.print("");
                    }
                }

            }
        }.start();
    }


    //判断是否能掷骰子

    public boolean isCanRoll() {
        return canRoll;
    }


    //设置可掷骰子

    public void setCanRoll(boolean canRoll) {
        this.canRoll = canRoll;
        if (canRoll) {
            this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        } else {
            this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        }
    }


}
